package com.itheima.app.mapper;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service
public class SettingManager {

    @Reference
    private QuestionService questionService;

    @Reference
    private NotificationService notificationService;

    @Reference
    private BlackListService blackListService;

    //查询通用设置
    public SettingVo findSettings(){
        //1.从ThreaLocal中获取到用户信息
        User user = UserHolder.getUser();

        // 2.根据用户id查询陌生人问题
        Question question = questionService.findByUserId(user.getId());

        //3.根据用户ID查询通知设置
        Notification notification = notificationService.findByUserId(user.getId());

        //4.组装查询
        SettingVo settingVo = new SettingVo();
        //设置id和phone
        settingVo.setId(user.getId());
        settingVo.setPhone(user.getPhone());
        //设置陌生人问题,先判断是否有问题
        if(question==null){
            settingVo.setStrangerQuestion("你喜欢高山还是大海？");
        }else{
            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        //设置通知
         if(notification==null){
             //设置默认通知
             settingVo.setLikeNotification(true);
             settingVo.setPinglunNotification(true);
             settingVo.setGonggaoNotification(true);
         }else{
             settingVo.setLikeNotification(notification.getLikeNotification());
             settingVo.setPinglunNotification(notification.getPinglunNotification());
             settingVo.setGonggaoNotification(notification.getGonggaoNotification());
         }
         return settingVo;
    }

    //保存或修改陌生人问题
    public void saveOrUpdateQuestions(String content) {
        //1.获取用户id
        Long userId =UserHolder.getUser().getId();

        //2.根据用户Id查询陌生人问题
        Question question = questionService.findByUserId(userId);

        //3.如果查询不到--新增
        if(question==null){
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionService.save(question);
        }else{
            //如果查询到了--修改
            question.setStrangerQuestion(content);
            questionService.update(question);
        }
    }

    //新增或修改用户的通知设置
    public void saveOrUpdateNotifications(Notification notificationParam) {
        //1.获取用户id
        Long userId = UserHolder.getUser().getId();

        // 2.根据用户id查询用户的通知设置
        Notification notification = notificationService.findByUserId(userId);

        //3. 如果查询不到--新增
        if(notification==null){
            notificationParam.setUserId(userId);
            notificationService.save(notificationParam);
        }else{
            // 如果查询到了--修改
            notificationParam.setId(notification.getId());
            notificationService.update(notificationParam);
        }
    }

    //分页查询黑名单列表
    public PageBeanVo findBlackList(Integer pageNum, Integer pageSize) {
        //获取登录用户id
        Long userId = UserHolder.getUser().getId();
        //调用service查询
        return blackListService.findListByPage(userId,pageNum,pageSize);
    }

    //移除黑名单用户
    public void deleteBlackUser(Long blackUserId) {
        //1. 获取登录用户id
        Long userId = UserHolder.getUser().getId();
        //2. 调用service移除
        blackListService.deleteByUserIdAndBlackUserId(userId, blackUserId);
    }

}
